package ccnt.pvc.scudwaremobile.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import ccnt.pvc.scudwaremobile.R;
import ccnt.pvc.scudwaremobile.dao.ServiceModel;
import ccnt.pvc.scudwaremobile.storage.ServiceDbManager;

public class ServiceListActivity extends Activity {

	private ViewPager mViewPager;
	private View view1, view2;
	private List<View> viewList = new ArrayList<View>(); 
	
	ServiceDbManager serviceDbManager = null;
	List<ServiceModel> regServices = null;
	List<ServiceModel> allServices = null;
	
	ListView listServiceAll;
	ListView listServiceReg;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_servicelist);
		
		initService();
		initView();
	}
	
	private void initService() {
		serviceDbManager = new ServiceDbManager(ServiceListActivity.this);
	}
	
	private void initView() {
		mViewPager = (ViewPager)findViewById(R.id.viewpager);
		
		LayoutInflater li = LayoutInflater.from(this);
		view1 = li.inflate(R.layout.activity_servicelist_reg, null);
		view2 = li.inflate(R.layout.activity_servicelist_all, null);
		
		viewList.add(view1);
		viewList.add(view2);
		
		PagerAdapter mPagerAdapter = new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return viewList.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager) container).removeView(viewList.get(position));
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(viewList.get(position));
				return viewList.get(position);
			}
		};
		
		mViewPager.setAdapter(mPagerAdapter);
		
		listServiceReg = (ListView)view1.findViewById(R.id.listServiceReg);
		fillListReg();
		listServiceAll = (ListView)view2.findViewById(R.id.listServiceAll);
		fillListAll();
	}
	
	private void fillListReg() {
		regServices = serviceDbManager.getAllRegServices();
		ArrayList<String> serviceNames = new ArrayList<String>();
		for(ServiceModel service : regServices) {
			serviceNames.add(service.getClassName());
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				ServiceListActivity.this, android.R.layout.simple_list_item_1,
				serviceNames);
		listServiceReg.setAdapter(adapter);	
		listServiceReg.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(ServiceListActivity.this, ServiceDetailActivity.class);
				intent.putExtra("className", regServices.get(arg2).getClassName());
				intent.putExtra("isReg", true);
				startActivity(intent);
			}
		});
	}
	
	private void fillListAll() {
		allServices = serviceDbManager.getAllServices();
		ArrayList<String> serviceNames = new ArrayList<String>();
		for(ServiceModel service : allServices) {
			serviceNames.add(service.getClassName());
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				ServiceListActivity.this, android.R.layout.simple_list_item_1,
				serviceNames);
		listServiceAll.setAdapter(adapter);
		listServiceAll.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(ServiceListActivity.this, ServiceDetailActivity.class);
				intent.putExtra("className", allServices.get(arg2).getClassName());
				intent.putExtra("isReg", false);
				startActivity(intent);
			}
		});
	}
}
